package org.softee.messaging.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CompoundProcessor implements Processor {
    private final List<Processor> delegates;

    public CompoundProcessor(Processor... delegates) {
        this(Arrays.asList(delegates));
    }

    public CompoundProcessor(List<Processor> delegates) {
        this.delegates = delegates;
    }

    @Override
    public List<String> process(String source) {
        List<String> messages = Collections.singletonList(source);
        for (Processor delegate : delegates) {
            List<String> results = new ArrayList<String>();
            for (String message : messages) {
                results.addAll(delegate.process(message));
            }
            messages = results;
        }
        return messages;
    }

}
